from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.db.models import permalink
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from tagging.fields import TagField

from ask.managers import AnswerPrivateManager, AnswerPublicManager
from ask.managers import QuestionPrivateManager, QuestionPublicManager

try:
    from django.conf.settings import MARKUP_CHOICES
except ImportError:
    MARKUP_CHOICES = (
        ('crl', _(u'Creole')),
        ('rst', _(u'reStructuredText')),
        ('txl', _(u'Textile')),
        ('mrk', _(u'Markdown')),
        ('htm', _(u'HTML')),
        ('txt', _(u'Plain text')),
    )

from django.conf import settings
if hasattr(settings,'CONTENT_STATUSES'):
    CONTENT_STATUSES=settings.CONTENT_STATUSES
    CONTENT_TR=settings.CONTENT_TR
else:
    from ask.managers import CONTENT_STATUSES, CONTENT_TR

class Question(models.Model):
    user = models.ForeignKey(User,related_name='my_questions')
    title = models.CharField(max_length=100)
    text = models.TextField()
    markup = models.CharField(_('markup'), max_length=3, default='mrk', choices=MARKUP_CHOICES)
    tags = TagField()
    to = models.ForeignKey(User,blank=True,null=True,related_name='questions_to_me')
    pub_date = models.DateTimeField(auto_now_add=True)
    edit_date = models.DateTimeField(auto_now=True)
    views = models.IntegerField(default=0,editable=True)
    status = models.IntegerField(choices=CONTENT_STATUSES, default=CONTENT_TR['public'])
    private = QuestionPrivateManager()
    objects = QuestionPublicManager()
    public = QuestionPublicManager()
    
    def do_view(self):
        self.views+=1
        self.save()
        return ''
        
    @models.permalink
    def get_absolute_url(self):
        return ('ask_question_detail',None, {'qid':self.id})

    @models.permalink
    def get_absolute_edit_url(self):
        return ('ask_question_edit',None, {'qid':self.id})

    @models.permalink
    def get_absolute_delete_url(self):
        return ('ask_question_delete',None, {'qid':self.id})

    def get_app(self):
        return self.__module__.split(".")[0]

    def get_model(self):
        return u'question'

    def get_publish(self):
        return self.pub_date
    
    def get_title(self):
        return self.__unicode__()
       
    @property
    def get_author(self):
        return self.user

    @property
    def get_text(self):
        return self.text
   
    def __unicode__(self):
        return self.title


class Answer(models.Model):
    user = models.ForeignKey(User)
    question = models.ForeignKey(Question)
    text = models.TextField()
    markup = models.CharField(_('markup'), max_length=3, default='mrk', choices=MARKUP_CHOICES)
    pub_date = models.DateTimeField(auto_now_add=True)
    edit_date = models.DateTimeField(auto_now=True)
    is_accepted = models.BooleanField(editable=False)
    status = models.IntegerField(choices=CONTENT_STATUSES, default=CONTENT_TR['public'])
    private = AnswerPrivateManager()
    objects = AnswerPublicManager()
    public = AnswerPublicManager()

    def get_absolute_url(self):
        return reverse('ask_question_detail',kwargs={'qid':self.question.id})

    def get_absolute_edit_url(self):
        return reverse('ask_answer_edit',kwargs={'aid':self.pk,'qid':self.question.id})

    def get_app(self):
        return self.__module__.split(".")[0]

    def get_model(self):
        return u'answer'

    def get_title(self):
        return self.__unicode__()

    def get_publish(self):
        return self.pub_date

    @property
    def get_author(self):
        return self.user
    
    def __unicode__(self):
        return self.text[0:40]
